package com.weixin.controller;


import com.github.pagehelper.PageHelper;
import com.weixin.entity.Employee;
import com.weixin.service.IEmployeeService;
import com.weixin.uitl.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gpf
 * @since 2018-12-24
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/list")
    public R listAll(@RequestParam(value = "pageNumber",defaultValue = "1")Integer pageNumber,@RequestParam(value = "pageSize",defaultValue = "6")Integer pageSize){
        /**
         * 查询所有
         */
        PageHelper.startPage(pageNumber,pageSize);
        List<Employee> employeeList = employeeService.list();
        /**
         * 查询总条目
         */
        int total = employeeService.count();
        return R.ok().put("total",total).put("rows",employeeList);
    }

    //根据id查询单条数据
    @GetMapping("id/{id}")
    public R selectById(@PathVariable Integer id, HttpServletRequest request){
        Employee byId = employeeService.getById(id);
        return R.ok().put("byid",byId);
    }

    /**
     *删除
     * @param id
     * @return
     */
    @PostMapping("/remove/{id}")
    public R removeById(@PathVariable Integer id){
        boolean b = employeeService.removeById(id);
        if (b){
            return R.ok("删除成功");
        }else {
            return R.error("删除失败");
        }
    }

    /**
     * 添加
     * @param employee
     * @return
     */
    @PostMapping("/save")
    public R save(@Valid Employee employee, BindingResult result){

        boolean b = employeeService.save(employee);
        if(b){
            return R.ok("添加成功");
        }else {
            return R.error("添加失败,信息输入有误或不符");
        }

    }

    /**
     * 条件查询
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/like")
    public R likeAll(@RequestParam(value = "pageNumber",defaultValue = "1")Integer pageNumber,@RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        /**
         * 查询所有
         */
        PageHelper.startPage(pageNumber,pageSize);
        List<Employee> employeeList = employeeService.list();
        /**
         * 查询总条目
         */
        int total = employeeService.count();
        return R.ok().put("total",total).put("rows",employeeList);
    }

    @PostMapping("/update")
    public R updateById(@Valid Employee employee, BindingResult result){
        System.out.println(employee);
        List<FieldError> fieldErrorslist = result.getFieldErrors();
        if(fieldErrorslist.size()>0){
            for (FieldError fieldErrors:fieldErrorslist
            ) {
                System.out.println(fieldErrors.getField()+"******"+fieldErrors.getDefaultMessage());
            }
        }
        boolean b = employeeService.updateById(employee);
        if (b){
            return R.ok("修改成功");
        }else {

            return R.error("修改失败");
        }
    }
}
